package com.hdoit.apollo.web.shop.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.apollo.logical.shop.model.Contact;
import com.hdoit.apollo.logical.shop.service.IContactService;
import com.hdoit.apollo.logical.viewmodel.shop.ContactSearch;
import com.tennetcn.authority.component.annotation.AuthPassport;
import com.tennetcn.authority.component.web.BaseController;
import com.tennetcn.authority.component.web.JsonActionResult;
import com.tennetcn.core.common.message.ManageContainer;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;
import com.tennetcn.web.message.WebManageContainer;

/**
 * @author      humaoshu
 * @email       
 * @createtime  2018年09月18日 10:25:11
 * @comment
 */
@Controller
@RequestMapping(value = "/shop/contact/")
public class ContactController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/shop/contact/";
    }

    @Autowired
    private IContactService contactService;

    private final String saveViewName="save";
    private final String indexViewName="index";
    private final String outputViewName="output";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,ContactSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(contactService.queryCount(search));
        List<Contact> contacts= contactService.queryList(search,pagerModel);

        model.addAttribute(WebManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("contacts", contacts);
        model.addAttribute("contactSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }
        return view(indexViewName);
    }
    
    @AuthPassport
    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Contact contact,String name,String tel,String message){
        if(StringUtils.isEmpty(contact.getId())){
            contact.setId(CommonUtils.getKeyId());
            contact.setName(name);
            contact.setTel(tel);
            contact.setMessage(message);
            contact.setCreateDate(DateUtils.getCurrent());
            contact.setModelStatus(ModelStatus.add);
        }else{
            contact.setModifyDate(DateUtils.getCurrent());
            contact.setModelStatus(ModelStatus.update);
        }

        if(contactService.applyChange(contact)){
        	return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        	
        }else{
        	return new JsonActionResult(ManageContainer.ERROR).toJson();
        }
    }
}